package org.springsource.roo.extrack.client.managed.activity;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;
import org.springsource.roo.extrack.client.managed.request.ApplicationRequestFactory;
import org.springsource.roo.extrack.client.managed.request.ExpenseProxy;
import org.springsource.roo.extrack.client.managed.request.ExpenseRequest;
import org.springsource.roo.extrack.client.managed.request.ReportProxy;
import org.springsource.roo.extrack.client.managed.ui.ExpenseDetailsView;
import org.springsource.roo.extrack.client.managed.ui.ExpenseEditView;
import org.springsource.roo.extrack.client.managed.ui.ExpenseListView;
import org.springsource.roo.extrack.client.managed.ui.ExpenseMobileDetailsView;
import org.springsource.roo.extrack.client.managed.ui.ExpenseMobileEditView;
import org.springsource.roo.extrack.client.scaffold.ScaffoldApp;
import org.springsource.roo.extrack.client.scaffold.place.CreateAndEditProxy;
import org.springsource.roo.extrack.client.scaffold.place.FindAndEditProxy;
import org.springsource.roo.extrack.client.scaffold.place.ProxyPlace;

public class ExpenseActivitiesMapper {

    private final ApplicationRequestFactory requests;

    private final PlaceController placeController;

    public ExpenseActivitiesMapper(ApplicationRequestFactory requests, PlaceController placeController) {
        this.requests = requests;
        this.placeController = placeController;
    }

    public Activity getActivity(ProxyPlace place) {
        switch(place.getOperation()) {
            case DETAILS:
                return new ExpenseDetailsActivity((EntityProxyId<ExpenseProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? ExpenseMobileDetailsView.instance() : ExpenseDetailsView.instance());
            case EDIT:
                return makeEditActivity(place);
            case CREATE:
                return makeCreateActivity();
        }
        throw new IllegalArgumentException("Unknown operation " + place.getOperation());
    }

    @SuppressWarnings("unchecked")
    private EntityProxyId<org.springsource.roo.extrack.client.managed.request.ExpenseProxy> coerceId(ProxyPlace place) {
        return (EntityProxyId<ExpenseProxy>) place.getProxyId();
    }

    private Activity makeCreateActivity() {
        ExpenseEditView.instance().setCreating(true);
        final ExpenseRequest request = requests.expenseRequest();
        Activity activity = new CreateAndEditProxy<ExpenseProxy>(ExpenseProxy.class, request, ScaffoldApp.isMobile() ? ExpenseMobileEditView.instance() : ExpenseEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ExpenseProxy proxy) {
                request.persist().using(proxy);
                return request;
            }
        };
        return new ExpenseEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ExpenseMobileEditView.instance() : ExpenseEditView.instance(), activity, null);
    }

    private Activity makeEditActivity(ProxyPlace place) {
        ExpenseEditView.instance().setCreating(false);
        EntityProxyId<ExpenseProxy> proxyId = coerceId(place);
        Activity activity = new FindAndEditProxy<ExpenseProxy>(proxyId, requests, ScaffoldApp.isMobile() ? ExpenseMobileEditView.instance() : ExpenseEditView.instance(), placeController) {

            @Override
            protected RequestContext createSaveRequest(ExpenseProxy proxy) {
                ExpenseRequest request = requests.expenseRequest();
                request.persist().using(proxy);
                return request;
            }
        };
        return new ExpenseEditActivityWrapper(requests, ScaffoldApp.isMobile() ? ExpenseMobileEditView.instance() : ExpenseEditView.instance(), activity, proxyId);
    }
}
